#include "KmyNodeImage.h"
#include "../KmyResourceManager.h"

KmyNodeImage::KmyNodeImage(int id, string name, int subimage,
                       int x, int y, int z,
                       float alpha, int flip, float angle)
{
    parent = NULL;

    this->id = id;
    this->name = name;
    this->subimage = subimage;
    imagepak = NULL;
    this->sx = 0;
    this->sy = 0;
    this->sw = -1;
    this->sh = -1;
    this->x = x;
    this->y = y;
    this->dw = -1;
    this->dh = -1;
    this->z = z;
    this->alpha = alpha;
    this->flip = flip;
    this->angle = angle;
}

KmyNodeImage::KmyNodeImage(int id, string name, int subimage,
           int sx, int sy, int sw, int sh,
           int dx, int dy, int dw, int dh, int z,
           float alpha, int flip, float angle)
{
    parent = NULL;

    this->id = id;
    this->name = name;
    this->subimage = subimage;
    imagepak = NULL;
    this->sx = sx;
    this->sy = sy;
    this->sw = sw;
    this->sh = sh;
    this->x = dx;
    this->y = dy;
    this->dw = dw;
    this->dh = dh;
    this->z = z;
    this->alpha = alpha;
    this->flip = flip;
    this->angle = angle;
}

void KmyNodeImage::finalize(){
    ST_VectorNode *p = child_list.getHead();
    while (p!=NULL){
        KmyNode* node = (KmyNode*)(p->value);
        node->finalize();
        delete node;
        p = p->next;
    }
    child_list.clear();
    if (parent!=NULL){
        parent->removeChild(this);
    }

}

int KmyNodeImage::getNodeType(){
    return KmyNode::NODE_TYPE_IMAGE;
}

void KmyNodeImage::render(KmyICanvas *screen){
    if (imagepak==NULL){
        KmyResourceManager *krm = KmyResourceManager::getInstance();
        imagepak = krm->getImage(name);
    }
    if (drawtask==NULL){
        drawtask = screen->addDrawTaskRegion(
                    imagepak, subimage,
                    sx, sy, sw, sh,
                    getGlobalX(), getGlobalY(), dw, dh, getGlobalZ(),
                    alpha, angle, flip
                    );
        is_updated = false;
    }else{
    //updata drawtask
        if (is_updated){
            drawtask->updateRegion(sx, sy, sw, sh,
                                   getGlobalX(), getGlobalY(), dw, dh, getGlobalZ(),
                                   angle, flip);
            is_updated = false;
        }
        screen->addDrawTask(drawtask);
    }
    ST_VectorNode *p = child_list.getHead();
    while (p!=NULL){
        KmyNode* node = (KmyNode*)(p->value);
        node->render(screen);
        p = p->next;
    }
}

void KmyNodeImage::rotate(float a){
    angle+=a;
    if (drawtask!=NULL){
        drawtask->updateAngle(angle);
    }
}

int KmyNodeImage::getFlip(){
	return flip;
}

void KmyNodeImage::setFlip(int f){
	this->flip = f;
    is_updated = true;
}

void KmyNodeImage::setAlpha(float a){
    this->alpha = a;
    if (drawtask!=NULL){
        drawtask->updateAlpha(this->alpha);
    }
}
